<?php

namespace App\Listeners;

use App\Events\Lottery;
use App\Http\Service\api\SMS;
use App\Repository\Repositories\LotteryRepositories;
use App\Repository\Repositories\UserStatisticisRepositories;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class SendSms
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\Lottery  $event
     * @return void
     */
    public function handle(Lottery $event)
    {
        //获取用户手机号
        $phone = (new UserStatisticisRepositories())->userPhone($event->user_id)[0]->phone;
        //获取用户姓名
        $name = (new UserStatisticisRepositories())->userPhoneName($phone)[0]->real_name;
        //获取商品名称
        $goods_name = (new LotteryRepositories())->searchGoods($event->goods_id)->store_name;
        if ($goods_name == '空'){
            //发送短信通知用户参数1：用户手机号，参数2：发送信息，参数3：发送模板
           $send_sms = SMS::sendTemplateSMS($phone,["【恭喜".$name."什么都没得到".'】',5],1);
            if ($send_sms){
                info('短信发送成功');
            }else{
                info($send_sms);
            }
        }else{
            $send_sms_have = SMS::sendTemplateSMS($phone,["【恭喜".$name."获得".$goods_name.'】',5],1);
            if ($send_sms_have){
                info('短信发送成功');
            }else{
                info($send_sms_have);
            }
        }
    }
}
